var TRE_FEE_TYPE_PATH = "";
var isExist = false;
function setPathPrefix(treFeeTypePath) {
	TRE_FEE_TYPE_PATH = treFeeTypePath;
}
var TRE_FEE_TYPE = function(){
	this.init();
};
/**
 * 初始化方法
 */
TRE_FEE_TYPE.prototype.init = function(){
	var self = this;
	self.expand();
	
	// 给select2组件加载样式
	WASP_WIDGET.initializeSelectStyle();
	// 初始化窗口标题名称
	setTitleName();
	
	$("#treFeeTypeAddForm").validate({
		focusCleanup : true
	});
	
	$("#feeCode").blur(function(){
		var feeCode = $("#feeCode").val();
		if(feeCode){
			var actionType = $("#actionType").val();
			if('add' == actionType){
				var actionUrl = TRE_FEE_TYPE_PATH + "isExist.do";
				$.ajax({
					type : "POST",
					url : actionUrl,
					dataType : "json",
					data : {feeCode : feeCode},
					success : function(data){
						if(data){
							isExist = true;
							toastr.warning("", "该费用编码已存在，请重新输入");
						}else{
							isExist = false;
						}
					},
					error : function(e) {
						toastr.error(e,"系统出现异常！");
					},
				});
			}
		}
	});
}
/**
 * 拓展方法
 */
TRE_FEE_TYPE.prototype.expand = function(){
	var self = this;
	
	self.validate = function() {
		$("#feeCode").rules("remove");
		$("#feeName").rules("remove");
		$("#feeLevel").rules("remove");
		$("#feeStandardName").rules("remove");
		
		$("#feeCode").rules("add", {
			required : true, maxlength4Byte: 10 , messages: {required: "请填写费用编码", maxlength4Byte:"费用编码不能大于10个字符"}
		});
		$("#feeName").rules("add", {
			required : true, maxlength4Byte: 100 , messages: {required: "请填写费用名称", maxlength4Byte:"费用名称不能大于100个字符"}
		});
		$("#feeLevel").rules("add", {
			required : true, messages: {required: "请选择费用层级"}
		});
		$("#feeStandardName").rules("add", {
			required : true, messages: {required: "请选择费用标准名称"}
		});
	};
	
	self.addTreFeeType = function() {
		SubmitAndPreventSecond("treFeeTypeAddForm",function() {
			self.validate();
			if(isExist){
				toastr.warning('', '该费用编码已存在，请重新输入');
				return false;
			}
			
			if(!$("form[name='treFeeTypeAddForm']").valid()) {
				toastr.warning('', '请完善信息');
				return false;
			}
			
			$('#treFeeTypeAddForm').attr('action', TRE_FEE_TYPE_PATH + "saveTreFeeType.do");
			
			return true;
		}, true);
	};
	
	self.updateTreFeeType = function() {
		SubmitAndPreventSecond("treFeeTypeAddForm",function() {
			self.validate();
			
			if(!$("form[name='treFeeTypeAddForm']").valid()) {
				toastr.warning('', '请完善信息');
				return false;
			}
			
			$('#treFeeTypeAddForm').attr('action', TRE_FEE_TYPE_PATH + "updateTreFeeType.do");
			
			return true;
		}, true);
	};
	
}

// 设置窗口标题名称
function setTitleName() {
	var titleName = "费用类型管理新增";
	var actionType = $("#actionType").val();
	if ("detail" == actionType) {
		titleName = "费用类型管理详情";
		$('input').attr("readOnly", "readOnly");
		$('button').hide();
		$('select').attr("disabled", "true");
	} else if ("update" == actionType) {
		titleName = "费用类型管理修改";
		$('#feeCode').attr("readOnly", "readOnly");
	}
	$('*[name="titleName"]').html(titleName);
}
// 刷新费用类型管理列表
function fleshParentGrid() {
	if (window.opener != null) {
		if (window.opener.document.btnRefresh != null) {
			window.opener.btnRefresh.click();
			if (window.opener.queryByCondtion != null) {
				window.opener.queryByCondtion(false);
			}
		} else {
			if (window.opener.queryByCondtion != null) {
				window.opener.queryByCondtion(false);
			}
		}
	}
}
